package com.exartisan.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.exartisan.bean.Album;
import com.exartisan.thanhdoanhn.PhotoActivity;
import com.exartisan.thanhdoanhn.R;
import com.exartisan.util.CommentDialog;
import com.exartisan.util.CommentDialog.onCommentListener;
import com.exartisan.util.DataHolder;
import com.exartisan.util.DateTimeFormat;
import com.exartisan.util.FbUtil;
import com.exartisan.util.Util;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GalleryAlbumAdapter extends BaseAdapter {

	Context context;
	ArrayList<Album> alAlbum;
	private HashMap<String, Integer> likeCount;
	private HashMap<String, Integer>  commentCount;
	
	
	public GalleryAlbumAdapter(Context context, ArrayList<Album> alAlbum) {
		this.context = context;
		this.alAlbum = alAlbum;
		likeCount = new HashMap<String, Integer>();
		commentCount = new HashMap<String, Integer>();
	}

	@Override
	public int getCount() {
		return alAlbum.size();
	}

	@Override
	public Object getItem(int pos) {
		return alAlbum.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return alAlbum.get(pos).getMediaID();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		final ViewHolder holder;
		View row;
		
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		if (convertView == null) {
			
			holder = new ViewHolder();
 
			//set row layout
			row = inflater.inflate(R.layout.row_photo_gallery, null);  
			
			holder.imgDesc = (ImageView) row.findViewById(R.id.imgDescription);
			holder.imgAvatar = (ImageView) row.findViewById(R.id.imgCreator);
			holder.tvTitle = (TextView) row.findViewById(R.id.tvTitle);
			holder.tvUserName = (TextView) row.findViewById(R.id.tvCreator);
			holder.tvCreatedTime = (TextView) row.findViewById(R.id.tvCreatedDate);
			holder.tvLikes = (TextView) row.findViewById(R.id.tvLikes);
			holder.tvComments = (TextView) row.findViewById(R.id.tvComments);
			holder.tvViewCount = (TextView) row.findViewById(R.id.tvViews);
			holder.layoutLikeGallery=(RelativeLayout) row.findViewById(R.id.layoutLikeGallery);
			holder.layoutComment = (RelativeLayout) row.findViewById(R.id.clickGallery);
			holder.imgLike=(ImageView) row.findViewById(R.id.btnLike);
			//declare
			row.setTag(holder);
 
		} else {
			row = (View)convertView;
			holder = (ViewHolder) row.getTag();
		}
			final Album a = alAlbum.get(position);
			
			//holder.imgAvatar.setImageUrl(a.getAvatar());
			//holder.imgDesc.setImageUrl(a.getImageThumb());
			
			ImageLoader.getInstance().displayImage(a.getAvatar(), holder.imgAvatar);
			ImageLoader.getInstance().displayImage(a.getImageThumb(), holder.imgDesc);
			
			holder.tvTitle.setText(a.getMediaTitlte());
			holder.tvCreatedTime.setText(DateTimeFormat.format(a.getDateCreate(), DateTimeFormat.SHORT_VALUE));
			holder.tvUserName.setText(a.getFullName());
			holder.tvViewCount.setText(a.getViews() + "");
			if (likeCount.containsKey(a.getFbFeedID())) {
				if (likeCount.get(a.getFbFeedID())!=0)
				holder.tvLikes.setText(likeCount.get(a.getFbFeedID()) + 
						"");
				else holder.tvLikes.setText("");
				}
			else Util.setLikeCount(a.getFbFeedID(), holder.tvLikes, likeCount);
			if (commentCount.containsKey(a.getFbFeedID())) {
				if (commentCount.get(a.getFbFeedID())!=0)
				holder.tvComments.setText(commentCount.get(a.getFbFeedID()) + "");
				else holder.tvComments.setText("");
			}
			else Util.setCommentCount(a.getFbFeedID(), holder.tvComments, commentCount);
			
			//fill data
			
			/*if (likeCount.containsKey(DataHolder.FEED_ID_SAMPLE_2))
				holder.tvLikes.setText(likeCount.get(DataHolder.FEED_ID_SAMPLE_2) + "");
			else
				setLikeCount(DataHolder.FEED_ID_SAMPLE_2, holder.tvLikes);
			if (commentCount.containsKey(DataHolder.FEED_ID_SAMPLE_2))
				holder.tvComments
						.setText(commentCount.get(DataHolder.FEED_ID_SAMPLE_2) + "");
			else
				setCommentCount(DataHolder.FEED_ID_SAMPLE_2, holder.tvComments);*/
			
			if(a.isLiked()) {
				holder.imgLike.setBackgroundResource(R.drawable.like_red);
				holder.tvLikes.setTextColor(Color.parseColor("#bf0000"));
			} else {
				holder.imgLike.setBackgroundResource(R.drawable.like_black);
				holder.tvLikes.setTextColor(Color.parseColor("#000000"));
			}
			
			row.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent i = new Intent(context, PhotoActivity.class);
					i.putExtra("type", 0);
					i.putExtra("id", a.getMediaID());
					DataHolder.setAlbumID(a.getMediaID());
					context.startActivity(i);
				}
			});
			holder.layoutLikeGallery.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					onClickLike(a, holder.imgLike,holder.tvLikes);
				}
			});
			
			holder.layoutComment.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					CommentDialog.show((Activity) context, a.getFbFeedID(), new onCommentListener() {
						
						@Override
						public void onSuccess() {
							Util.setCommentCount(a.getFbFeedID(), holder.tvComments, commentCount);
						}
					});
				}
			});
			
		return row;
	}
	
	public void flushLikeCmtCount(){
		this.likeCount.clear();
		this.commentCount.clear();
	}
	
    
    private void onClickLike(final Album album, final ImageView btnLike, final TextView tvLikes) {
		if(album.isLiked()) {
			FbUtil.unLikeId(album.getFbFeedID(), (Activity) context, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(int arg0, String arg1) {
				super.onSuccess(arg0, arg1);
				Util.setLikeCount(album.getFbFeedID(), tvLikes, likeCount);
				album.setLiked(false);
				btnLike.setBackgroundResource(R.drawable.like_black);
				tvLikes.setTextColor(Color.parseColor("#000000"));
			}
			
			@Override
			public void onFailure(Throwable arg0, String arg1) {
				super.onFailure(arg0, arg1);
			}
			
		});
			
		} else
		FbUtil.likeId(album.getFbFeedID(), (Activity) context,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(int arg0, String response) {
						super.onSuccess(arg0, response);
						Util.setLikeCount(album.getFbFeedID(), tvLikes, likeCount);
						album.setLiked(true);
						btnLike.setBackgroundResource(R.drawable.like_red);
						tvLikes.setTextColor(Color.parseColor("#bf0000"));
					}

					@Override
					public void onFailure(Throwable arg0, String arg1) {
						super.onFailure(arg0, arg1);
					}
				});
	}
	
	private class ViewHolder{
		ImageView  imgDesc,imgAvatar,imgLike;
		TextView tvTitle, tvUserName, tvCreatedTime, tvLikes, tvComments, tvViewCount;
		RelativeLayout layoutLikeGallery, layoutComment;
	}

}
